package com.tennis.tennisreservation.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Paths;

/**
 * Web配置类
 * 配置静态资源映射，使上传的文件可以通过URL访问
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Value("${file.upload-dir:./uploads}")
    private String uploadDir;
    
    @Value("${file.access-path:/uploads/}")
    private String accessPath;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加资源处理器，将URL请求映射到文件系统的物理位置
        String uploadPath = Paths.get(uploadDir).toAbsolutePath().toString();
        
        // 确保路径以斜杠结束
        if (!uploadPath.endsWith("/")) {
            uploadPath += "/";
        }
        
        // 映射资源路径
        registry.addResourceHandler(accessPath + "**")
                .addResourceLocations("file:" + uploadPath);
    }
}
